package com.usernet.product.web.action;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.usernet.product.comm.BaseAction;
import com.usernet.product.entity.Admin;
import com.usernet.product.entity.Users;
import com.usernet.product.utils.LogConstants;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;
import com.usernet.product.utils.ProductUtils;
import com.usernet.product.web.form.UserForm;

public class UserAction extends BaseAction {

	public ActionForward toList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm mForm = (UserForm) form;
		// 查询条件
		String loginName = mForm.getLoginName();

		PageResult<Users> result = null;
		Page page = new Page();
		// 分页信息
		String pageNo = mForm.getPageNo();
		String pageSize = mForm.getPageSize();
		if (pageSize != null) {
			page.setEveryPage(Integer.parseInt(pageSize.trim()));
		}
		if (pageNo != null) {
			page.setCurrentPage(Integer.parseInt(pageNo.trim()));
		}
		// 条件查询
		if (loginName != null) {
			if (loginName.trim().equals("")) {
				result = userDao.getAllByPage(page);
			} else {
				// 有条件
				result = userDao.getAllByParam(page, loginName);
			}
		} else {

			result = userDao.getAllByPage(page);
		}
		request.setAttribute("form", mForm);
		request.setAttribute("list", result.getContent());
		request.setAttribute("page", result.getPage());
		request.setAttribute("size", result.getSize());

		return mapping.findForward("list");
	}

	/*
	 * 锁定用户
	 */
	public ActionForward doCancle(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int userId = Integer.parseInt(request.getParameter("id"));
		Users user = (Users) userDao.getById(Users.class, userId);
		user.setStatus(1);// 不可用
		if (userDao.modify(user)) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.lock_user, new Object[] {
							user.getId(), user.getLoginName() }), null);
		}
		return mapping.findForward("other");
	}

	/*
	 * 解锁用户
	 */
	public ActionForward doOk(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int userId = Integer.parseInt(request.getParameter("id"));
		Users user = (Users) userDao.getById(Users.class, userId);
		user.setStatus(0);// 解锁
		if (userDao.modify(user)) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.unlock_user, new Object[] {
							user.getId(), user.getLoginName() }), null);
		}
		return mapping.findForward("other");
	}

	/** to添加用户 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		return mapping.findForward("add");
	}

	/** to修改用户 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Users user = (Users) userDao.getById(Users.class, id);
		request.setAttribute("user", user);
		return mapping.findForward("update");
	}

	/** 添加用户 */
	public ActionForward doAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String loginName = request.getParameter("loginName").trim();
		boolean bool = userDao.checkLoginName(loginName);
		if (bool) {
			request.setAttribute("msg", "登录名已经存在");
			request.setAttribute("return", "user.do?method=toAdd");
			return mapping.findForward("admin_msg");
		}
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String niceName = request.getParameter("niceName");
		String tel = request.getParameter("tel");
		int flag = Integer.parseInt(request.getParameter("flag"));
		Users user = new Users();
		user.setLoginName(loginName);
		user.setPassword(ProductUtils.getMD5String(password));
		user.setEmail(email);
		user.setNiceName(niceName);
		user.setTel(tel);
		user.setTime(new Timestamp(System.currentTimeMillis()));
		user.setStatus(0); // 状态可用
		user.setFlag(flag); // 标识
		int id = userDao.save(user);
		if (id > 0) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.add_user, new Object[] { id,
							user.getLoginName() }), null);
			request.setAttribute("msg", "添加成功");
		} else {
			request.setAttribute("msg", "添加失败");

		}
		request.setAttribute("return", "user.do?method=toList");
		return mapping.findForward("admin_msg");
	}

	/** 修改用户 */
	public ActionForward doUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String niceName = request.getParameter("niceName");
		String tel = request.getParameter("tel");
		int flag = Integer.parseInt(request.getParameter("flag"));
		Users user = (Users) userDao.getById(Users.class, id);
		user.setPassword(ProductUtils.getMD5String(password));
		user.setEmail(email);
		user.setFlag(flag);
		user.setNiceName(niceName);
		user.setTel(tel);
		if (userDao.modify(user)) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.update_user, new Object[] {
							user.getId(), user.getLoginName() }), null);
			request.setAttribute("msg", "修改成功");
		} else {
			request.setAttribute("msg", "修改失败");

		}
		request.setAttribute("return", "user.do?method=toList");
		return mapping.findForward("admin_msg");
	}

	/** 删除用户 */
	public ActionForward doDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Users user = (Users) userDao.getById(Users.class, id);
		if (userDao.delete(user)) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.delete_user, new Object[] {
							user.getId(), user.getLoginName() }), null);
			return mapping.findForward("other");
		} else {
			request.setAttribute("msg", "删除失败");
			request.setAttribute("return", "user.do?method=toList");
			return mapping.findForward("admin_msg");
		}
	}
}
